﻿// -----------------------------------------------------------------------
// <copyright file="EmployeeRepository.cs" company="AGR">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace AGR.Data.Repository.Contract
{
    using System;
    using System.Linq;
    using AGR.Model;

    /// <summary>
    /// Employee data access
    /// </summary>
    public class EmployeeRepository : AbstractRepository<Employee>
    {
        public EmployeeRepository(ContextHelper contextHelper)
        {
            base._contextHelper = contextHelper;
            base._context = _contextHelper.GetContext();
        }

        public override IQueryable<Employee> GetAll()
        {
            var result = (from x in base._context.Employees
                          select new Employee
                          {
                              Id = x.Id,
                              EmployeeCode = x.EmployeeCode,
                              FirstName = x.FirstName,
                              LastName = x.LastName,
                              Gender = x.Gender,
                              IdNumber = x.IdNumber,
                              Type = x.Type,
                              IsTrainer = x.IsTrainer,
                              TrainerSpecialization = x.TrainerSpecialization,
                              HireDate = x.HireDate,
                              PhysicalAddress = x.PhysicalAddress,
                              PostalAddress = x.PostalAddress,
                              LoginUserId = x.LoginUserId,
                              LoginUserName = x.LoginUserName,
                              LoginPassword = x.LoginPassword,
                              LoginLocked = x.LoginLocked,
                              DateCreated = x.DateCreated,
                              UserCreated = x.UserCreated,
                              DateModified = x.DateModified,
                              UserModified = x.UserModified
                          });

            return result;
        }

        public override void Add(Employee entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Employee entityToAdd = new Context.Employee
            {
                Id = entity.Id,
                EmployeeCode = entity.EmployeeCode,
                FirstName = entity.FirstName,
                LastName = entity.LastName,
                Gender = entity.Gender,
                IdNumber = entity.IdNumber,
                Type = entity.Type,
                IsTrainer = entity.IsTrainer,
                TrainerSpecialization = entity.TrainerSpecialization,
                HireDate = entity.HireDate,
                PhysicalAddress = entity.PhysicalAddress,
                PostalAddress = entity.PostalAddress,
                LoginUserId = entity.LoginUserId,
                LoginUserName = entity.LoginUserName,
                LoginPassword = entity.LoginPassword,
                LoginLocked = entity.LoginLocked,
                DateCreated = DateTime.Now,
                UserCreated = entity.UserCreated,
                DateModified = null,
                UserModified = null
            };
           
            base._context.Employees.InsertOnSubmit(entityToAdd);
        }

        public override void Update(Employee entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Employee entityToUpdate = base._context.Employees.Single(x => x.EmployeeCode.Equals(entity.EmployeeCode));

            entityToUpdate.Id = entity.Id;
            entityToUpdate.EmployeeCode = entity.EmployeeCode;
            entityToUpdate.FirstName = entity.FirstName;
            entityToUpdate.LastName = entity.LastName;
            entityToUpdate.Gender = entity.Gender;
            entityToUpdate.IdNumber = entity.IdNumber;
            entityToUpdate.Type = entity.Type;
            entityToUpdate.IsTrainer = entity.IsTrainer;
            entityToUpdate.TrainerSpecialization = entity.TrainerSpecialization;
            entityToUpdate.HireDate = entity.HireDate;
            entityToUpdate.PhysicalAddress = entity.PhysicalAddress;
            entityToUpdate.PostalAddress = entity.PostalAddress;
            entityToUpdate.LoginUserId = entity.LoginUserId;
            entityToUpdate.LoginUserName = entity.LoginUserName;
            entityToUpdate.LoginPassword = entity.LoginPassword;
            entityToUpdate.LoginLocked = entity.LoginLocked;
            entityToUpdate.DateModified = DateTime.Now;
            entityToUpdate.UserModified = entity.UserModified;
        }

        public override void Delete(Employee entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Employee entityToDelete = base._context.Employees.Single(x => x.EmployeeCode.Equals(entity.EmployeeCode));

            base._context.Employees.DeleteOnSubmit(entityToDelete);
        }
    }
}
